/*
 * CoreMessages.java
 *
 *  
 */
package org.msb.finance.res;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * This class loads a {@link ResourceBundle} object that contains the localizable resources used by the
 * {@code MSB Finance Core} component.
 * 
 * The default base name of the bundle can be overridden by specify a system property (with the {@code -D} command line
 * argument) named {@value CoreMessages#SYSTEM_PROPERTY_KEY} and setting its value to the desired bundle base name.
 * <strong>NOTE:</strong> the {@link ResourceBundle} is loaded as a resource using a {@link ClassLoader} and not as a
 * file, so the specified bundle must appear on the class path.
 * 
 * @author Marc Boudreau
 */
@SuppressWarnings("nls")
public class CoreMessages {

	/*
	 * The underlying bundle.
	 */
	private static ResourceBundle bundle;

	/*
	 * A constant containing the name of the System property to check for an override.
	 */
	private static final String SYSTEM_PROPERTY_KEY = "org.msb.finance.res.CoreMessages.bundleName";

	/*
	 * A constant containing the default name for the bundle.
	 */
	private static final String DEFAULT_BUNDLE_NAME = "org.msb.finance.res.msbfincoremsgs";

	/*
	 * Loads the underlying ResourceBundle used by this class.
	 */
	static {
		String bundleName = DEFAULT_BUNDLE_NAME;

		try {
			bundleName = System.getProperty(SYSTEM_PROPERTY_KEY, DEFAULT_BUNDLE_NAME);
		} catch (SecurityException ex) {
			/*
			 * Once a logging infrastructure is in place, log a message indicating that the SecurityManager isn't
			 * allowing the org.msb.finance.res.CoreMessages.bundleName property from being read, meaning that the
			 * default bundle name cannot be overridden.
			 */
		}

		bundle = ResourceBundle.getBundle(bundleName);
	}

	/**
	 * Returns the {@link String} object contained in the underlying {@link ResourceBundle} for the given key.
	 * 
	 * @param key
	 *            A {@link String} object containing the key of the desired {@link String} object resource.
	 * 
	 * @return A {@link String} object containing the desired resource, or the value of the {@code key} parameter
	 *         surrounded by exclamation marks if the underlying bundle doesn't contain the desired resource.
	 */
	public static String getString(String key) {
		try {
			return bundle.getString(key);
		} catch (MissingResourceException ex) {
			return "!" + key + "!";
		}
	}
}
